package com.example.eshop.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.eshop.dao.EshopOrderMainDAO;
import com.example.eshop.entity.EshopOrderCustomer;
import com.example.eshop.entity.EshopOrderMain;
import com.example.eshop.entity.EshopOrderTicket;
import com.example.eshop.service.EshopOrderCustomerService;
import com.example.eshop.service.EshopOrderMainService;
import com.example.eshop.service.EshopOrderTicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author jeeweb
 * @version V1.0
 * @Title: 订单主表
 * @Description: 订单主表
 * @date 2018-10-01 11:45:18
 */

@Service
@CacheConfig(cacheNames = "EshopOrderMainCache")
public class EshopOrderMainServiceImpl extends ServiceImpl<EshopOrderMainDAO, EshopOrderMain> implements EshopOrderMainService {


    @Autowired
    private EshopOrderTicketService eshopOrderTicketService;
    @Autowired
    private EshopOrderCustomerService eshopOrderCustomerService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    /**
     * @Cacheable 会先查询缓存，如果缓存中存在，则不执行方法
     */
    @Cacheable(key = "#p0")
    @Override
    public EshopOrderMain getOrderMainById(Long id) {

        EshopOrderMain eshopOrderMain = baseMapper.getOrderMainById(id);


        return eshopOrderMain;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CachePut(key = "#p0.id")
    public EshopOrderMain updateOrderMain(EshopOrderMain eshopOrderMain) {

        List<EshopOrderTicket> eshopOrderTicketList = eshopOrderMain.getEshopOrderTicketList();

        EshopOrderCustomer eshopOrderCustomer = eshopOrderMain.getCustomerId();
        baseMapper.updateById(eshopOrderMain);
        eshopOrderCustomerService.saveOrUpdate(eshopOrderCustomer);
        eshopOrderMain.setCustomerId(eshopOrderCustomer);
        eshopOrderTicketService.saveOrUpdateBatch(eshopOrderTicketList);
        //可能只是更新某几个字段而已，所以查次数据库把数据全部拿出来全部

        return baseMapper.getOrderMainById(eshopOrderMain.getId());

    }


    /**
     * 因为必须要有返回值，才能保存到数据库中，如果保存的对象的某些字段是需要数据库生成的，
     * 那保存对象进数据库的时候，就没必要放到缓存了
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CachePut(key = "#p0.id")
    public EshopOrderMain saveOrderMain(EshopOrderMain eshopOrderMain) {


        List<EshopOrderTicket> eshopOrderTicketList = eshopOrderMain.getEshopOrderTicketList();

        EshopOrderCustomer eshopOrderCustomer = eshopOrderMain.getCustomerId();
        eshopOrderCustomerService.save(eshopOrderCustomer);
        eshopOrderMain.setCustomerId(eshopOrderCustomer);

        baseMapper.insert(eshopOrderMain);
        for (EshopOrderTicket eshopOrderTicket : eshopOrderTicketList) {
            eshopOrderTicket.setOrder(eshopOrderMain.getId());
            eshopOrderTicketService.save(eshopOrderTicket);
        }

        return baseMapper.getOrderMainById(eshopOrderMain.getId());


    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "#p0")
    public boolean removeById(Long id) {

        EshopOrderMain eshopOrderMain = baseMapper.getOrderMainById(id);
        baseMapper.deleteById(id);
        QueryWrapper ticketWrapper = new QueryWrapper();
        ticketWrapper.eq("order_id", eshopOrderMain.getId());
        eshopOrderTicketService.remove(ticketWrapper);

        QueryWrapper customerWrapper = new QueryWrapper();
        customerWrapper.eq("id", eshopOrderMain.getCustomerId().getId());
        eshopOrderCustomerService.remove(customerWrapper);
        return true;
    }



}
